package com.yunxiao.cloud.sharding.algorithm;

import com.yunxiao.cloud.sharding.util.HashUtil;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * @description: 分表算法
 * @author: yunxiao
 * @date: 2020/1/2 10:16
 * @version: V1.0
 **/
public class TableOneAlgorithm implements PreciseShardingAlgorithm<String> {
    private static Logger logger = LoggerFactory.getLogger(TableOneAlgorithm.class);

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        logger.debug("分表算法参数 {},{}", collection, preciseShardingValue);
        int hash = HashUtil.rsHash(String.valueOf(preciseShardingValue.getValue()));
        return "table_one_" + (hash % 5+1);
    }
}
